package com.hang.ixin.controller;

import com.hang.ixin.Enums.AddFriendStatusEnum;
import com.hang.ixin.Enums.MsgSignEnum;
import com.hang.ixin.Enums.OptStatusEnum;
import com.hang.ixin.POJO.ChatMsg;
import com.hang.ixin.POJO.DTO.FriendsRequestDTO;
import com.hang.ixin.POJO.DTO.UsersDTO;
import com.hang.ixin.POJO.FriendsRequest;
import com.hang.ixin.POJO.MyFriends;
import com.hang.ixin.POJO.Users;
import com.hang.ixin.POJO.VO.UsersVO;
import com.hang.ixin.service.impl.UserServiceImpl;
import com.hang.ixin.utils.FastDFSClient;
import com.hang.ixin.utils.FileUtils;
import com.hang.ixin.utils.JSONResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserServiceImpl userService;
    @Autowired
    FastDFSClient fastDFSClient;

    @PostMapping("/login")
    public JSONResult login(@RequestBody Users user) {

        Users users = userService.login(user.getUsername(), user.getPassword());
        if (users == null) {
            return JSONResult.errorMsg("用户名或密码错误");
        }
        UsersVO usersVO = new UsersVO();
        BeanUtils.copyProperties(users, usersVO);
        return JSONResult.ok(usersVO);
    }

    @PostMapping("/uploadFace")
    public JSONResult uploadFace(@RequestBody UsersDTO usersDTO) throws Exception {

        String faceImage = usersDTO.getFaceImage();
        String filePath = "c://" + usersDTO.getId() + "faceImage64.png";
        FileUtils.base64ToFile(filePath, faceImage);

        MultipartFile multipartFile = FileUtils.fileToMultipart(filePath);
        String urlBig = fastDFSClient.uploadBase64(multipartFile);

        String thump = "_150x150.";
        String[] arr = urlBig.split("\\.");
        String urlSmall = arr[0] + thump + arr[1];


        Users users = userService.findOne(usersDTO.getId());
        users.setFaceImage(urlSmall);
        users.setFaceImageBig(urlBig);

        Users users1 = userService.updateImage(users);

        UsersVO usersVO = new UsersVO();
        BeanUtils.copyProperties(users1, usersVO);
        return JSONResult.ok(usersVO);
    }

    @PostMapping("/uploadNickname")
    public JSONResult uploadNickname(@RequestBody UsersDTO usersDTO) {
        Users users = userService.findOne(usersDTO.getId());
        users.setNickname(usersDTO.getNickname());

        Users users1 = userService.updateNickname(users);
        UsersVO usersVO = new UsersVO();
        BeanUtils.copyProperties(users1, usersVO);
        return JSONResult.ok(usersVO);

    }

    @PostMapping("/search")
    public JSONResult search(String myId, String friendUsername) {
        if (StringUtils.isBlank(friendUsername)) {
            return JSONResult.errorMsg("用户名不合法");
        }

        int code = userService.findInConditions(myId, friendUsername);


        if (code == AddFriendStatusEnum.SUCCESS.getCode()) {
            Users users = userService.findUsersByUsername(friendUsername);
            UsersVO usersVO = new UsersVO();
            BeanUtils.copyProperties(users, usersVO);
            return JSONResult.ok(usersVO);
        } else {
            return JSONResult.errorMsg(AddFriendStatusEnum.getMsgByCode(code));
        }

    }

    @PostMapping("/add")
    public JSONResult add(String myId, String friendUsername) {
        if (StringUtils.isBlank(myId) || StringUtils.isBlank(friendUsername)) {
            return JSONResult.errorMsg("添加用户名不能为空");
        }

        Integer code = userService.findInConditions(myId, friendUsername);
        if (code == AddFriendStatusEnum.SUCCESS.getCode()) {

            Users friend = userService.findUsersByUsername(friendUsername);
            userService.requestExist(myId, friend.getId());
            return JSONResult.ok();

        } else {
            return JSONResult.errorMsg(AddFriendStatusEnum.getMsgByCode(code));
        }
    }

    @PostMapping("/friendsRequest")
    public JSONResult friendsRequest(String acceptUserId) {

        if (StringUtils.isBlank(acceptUserId)) {
            return JSONResult.errorMsg("");
        }

        List<String> arr = new ArrayList<>();
        List<FriendsRequest> list = userService.friendsRequest(acceptUserId);
        for (FriendsRequest request : list) {
            String id = request.getSendUserId();
            arr.add(id);
        }
        List<Users> usersList = userService.findAllByIdIn(arr);
        List<FriendsRequestDTO> dtoList = new ArrayList<>();
        for (Users users : usersList) {
            FriendsRequestDTO fDTO = new FriendsRequestDTO();
            BeanUtils.copyProperties(users, fDTO);
            dtoList.add(fDTO);
        }

        return JSONResult.ok(dtoList);

    }

    @PostMapping("/reqHandle")
    public JSONResult reqHandle(String myId, String acceptId, Integer optStatus) {
        if (StringUtils.isBlank(myId) || StringUtils.isBlank(acceptId) || optStatus == null) {
            return JSONResult.errorMsg("");
        }
        if (OptStatusEnum.getMsgByStatus(optStatus) == null) {
            return JSONResult.errorMsg("");
        }

        if (optStatus.equals(OptStatusEnum.IGNORE.getStatus())) {
            // 忽略请求
            userService.deleteReq(acceptId, myId);

        }
        if (optStatus.equals(OptStatusEnum.PASS.getStatus())) {
            // 通过请求
            userService.passReq(acceptId, myId);
            //返回朋友列表
            List<MyFriends> list = userService.findFriends(myId);
            List<FriendsRequestDTO> dtoList = new ArrayList<>();
            List<String> ids = new ArrayList<>();

            for (MyFriends friends : list) {
                ids.add(friends.getMyFriendUserId());
            }
            List<Users> friendsList = userService.findAllByIdIn(ids);
            for (Users user : friendsList) {
                FriendsRequestDTO friendsRequestDTO = new FriendsRequestDTO();
                BeanUtils.copyProperties(user, friendsRequestDTO);
                dtoList.add(friendsRequestDTO);
            }
            return JSONResult.ok(dtoList);

        }
        return JSONResult.ok();

    }

    @PostMapping("/myFriends")
    public JSONResult myFriends(String myId) {
        if (StringUtils.isBlank(myId)) {
            return JSONResult.errorMsg("");
        }

        List<MyFriends> list = userService.findFriends(myId);
        List<FriendsRequestDTO> dtoList = new ArrayList<>();
        List<String> ids = new ArrayList<>();

        for (MyFriends friends : list) {
            ids.add(friends.getMyFriendUserId());
        }
        List<Users> friendsList = userService.findAllByIdIn(ids);
        for (Users user : friendsList) {
            FriendsRequestDTO friendsRequestDTO = new FriendsRequestDTO();
            BeanUtils.copyProperties(user, friendsRequestDTO);
            dtoList.add(friendsRequestDTO);
        }
        return JSONResult.ok(dtoList);
    }

    @PostMapping("/getUnsignMsg")
    public JSONResult getUnsignMsg(String myId) {
        if (StringUtils.isBlank(myId)){
            return JSONResult.errorMsg("");
        }
        //获取未读消息
        List<ChatMsg> chatMsgList = userService.getUnsignMsg(myId, MsgSignEnum.UNSIGNED.getCode());

        return JSONResult.ok(chatMsgList);
    }
}
